<?xml version="1.0" encoding="utf-8"?>
<!--
   This file is a part of dcstore project,
   licensed under GPLv2
  
   Dominik Cebula
   dominikcebula@gmail.com
-->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:c="http://java.sun.com/jsp/jstl/core">
    <h:body>
        <ui:composition template="admin-template.xhtml">
            <ui:define name="main">                
                <table id="tbl_product" class="actions">
                    <thead>
                        <tr>
                            <th class="col1">id</th>
                            <th class="col2">name</th>
                            <th class="col3">category</th>
                            <th class="col4">price</th>
                            <th class="col5">tax</th>
                            <th class="col6">price with tax</th>
                            <th class="col7">actions</th>
                        </tr>
                    </thead>
                    <tbody>
                        <c:forEach items="#{productWeb.all}"
                                   var="p">
                            <tr>
                                <c:choose>
                                    <c:when test="#{request.getParameter('id')==p.id}">
                                        <h:form>                                            
                                            #{productWeb.setNameEdit(p.name)}
                                            #{productWeb.setIdCategoryEdit(p.category.id)}
                                            #{productWeb.setPriceEdit(p.price)}
                                            #{productWeb.setIdTaxEdit(p.tax.id)}
                                            #{productWeb.setPriceWithTaxEdit(p.priceWithTax)}

                                            <td class="col1">#{p.id}</td>
                                            <td class="col2">
                                                <h:inputText required="true"
                                                             requiredMessage="Product name is required"
                                                             validatorMessage="Wrong data in product name field"
                                                             value="#{productWeb.nameEdit}" />
                                            </td>
                                            <td class="col3">
                                                <h:selectOneMenu required="true"
                                                                 requiredMessage="Please select category for this product"
                                                                 value="#{productWeb.idCategoryEdit}">
                                                    <f:selectItems value="#{categoryWeb.all}"
                                                                   var="c"
                                                                   itemLabel="#{c.name}"
                                                                   itemValue="#{c.id}" />
                                                </h:selectOneMenu>
                                            </td>                                            
                                            <td>
                                                <h:inputText id="priceEdit"
                                                             value="#{productWeb.priceEdit}"
                                                             required="true"
                                                             validatorMessage="Invalid price value"
                                                             requiredMessage="Price field is required"
                                                             onkeyup="calcPrice(this.form, false, 'Edit')" />
                                            </td>
                                            <td>
                                                <h:selectOneMenu id="taxEdit"
                                                                 value="#{productWeb.idTaxEdit}"
                                                                 required="true"
                                                                 requiredMessage="Tax is required"
                                                                 onchange="calcPrice(this.form, false, 'Edit')">
                                                    <f:selectItems value="#{taxWeb.all}"
                                                                   var="t"
                                                                   itemLabel="#{t.rate}"
                                                                   itemValue="#{t.id}" />
                                                </h:selectOneMenu>
                                            </td>
                                            <td>
                                                <h:inputText id="priceWithTaxEdit"
                                                             value="#{productWeb.priceWithTaxEdit}"
                                                             required="true"
                                                             validatorMessage="Invalid price value"
                                                             requiredMessage="Price field is required"
                                                             onkeyup="calcPrice(this.form, true, 'Edit')" />
                                            </td>
                                            <td>
                                                #{productWeb.setId(p.id)}
                                                <h:inputHidden value="#{productWeb.id}" />
                                                <h:commandLink action="#{productWeb.edit()}"
                                                               value="Save">
                                                    <f:param name="id" value="#{p.id}" />
                                                </h:commandLink>
                                            </td>
                                        </h:form>
                                    </c:when>
                                    <c:otherwise>
                                        <td class="col1">#{p.id}</td>
                                        <td class="col2">#{p.name}</td>
                                        <td class="col3">#{p.category.name}</td>
                                        <td class="col4">#{p.price}</td>
                                        <td class="col5">#{p.tax.rate}</td>
                                        <td class="col6">#{p.priceWithTax}</td>
                                        <td class="col7">
                                            <h:form>
                                                <h:link value="Edit">
                                                    <f:param name="id" value="#{p.id}" />
                                                </h:link>
                                                &nbsp;
                                                <h:commandLink value="Delete"
                                                               action="#{productWeb.delete(p.id)}"
                                                               onclick="return confirm('Do you really want to delete this product?')" />
                                                &nbsp;
                                                <h:commandLink value="Images"
                                                               action="#{imagesWeb.go(p.id)}" />
                                                &nbsp;
                                                <h:commandLink value="Description"
                                                               action="#{descriptionWeb.go(p.id)}" />
                                            </h:form>
                                        </td>
                                    </c:otherwise>
                                </c:choose>
                            </tr>
                        </c:forEach>
                    </tbody>
                    <tfoot>
                        <tr>
                            <h:form>
                                <td class="col1"></td>
                                <td class="col2">
                                    <h:inputText required="true"
                                                 requiredMessage="Product name is required"
                                                 validatorMessage="Wrong data in product name field"
                                                 value="#{productWeb.name}" />
                                </td>                                
                                <td class="col3">
                                    <h:selectOneMenu required="true"
                                                     requiredMessage="Please select category for this product"
                                                     value="#{productWeb.idCategory}">
                                        <f:selectItems value="#{categoryWeb.all}"
                                                       var="p"
                                                       itemLabel="#{p.name}"
                                                       itemValue="#{p.id}" />
                                    </h:selectOneMenu>
                                </td>
                                <td class="col4">
                                    <h:inputText id="priceNew"
                                                 required="true"
                                                 requiredMessage="Price field is required"
                                                 validatorMessage="Wrong price"
                                                 value="#{productWeb.price}"
                                                 onkeyup="calcPrice(this.form, false, 'New')" />
                                </td>
                                <td class="col5">
                                    <h:selectOneMenu id="taxNew"
                                                     value="#{productWeb.idTax}"
                                                     required="true"
                                                     requiredMessage="Tax is required"
                                                     onchange="calcPrice(this.form, false, 'New')">
                                        <f:selectItems value="#{taxWeb.all}"
                                                       var="t"
                                                       itemLabel="#{t.rate}"
                                                       itemValue="#{t.id}" />
                                    </h:selectOneMenu>
                                </td>
                                <td class="col6">
                                    <h:inputText id="priceWithTaxNew"
                                                 required="true"
                                                 requiredMessage="Price with tax field is required"
                                                 validatorMessage="Wrong price with tax"
                                                 value="#{productWeb.priceWithTax}"
                                                 onkeyup="calcPrice(this.form, true, 'New')" />
                                </td>
                                <td class="col7">
                                    <h:commandLink action="#{productWeb.add()}" 
                                                   value="Add" />
                                </td>
                            </h:form>
                        </tr>
                    </tfoot>
                </table>
            </ui:define>
        </ui:composition>
    </h:body>
</html>